package com.cabrida.livestar.main;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

import com.cabrida.livestar.BaseActivity;
import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.config.HttpHelp;
import com.cabrida.livestar.config.Urls;
import com.cabrida.livestar.model.JSONParser;
import com.cabrida.livestar.util.HttpUtils;
import com.cabrida.livestar.util.JsonUtils;
import com.cabrida.livestar.util.L;
import com.cabrida.livestar.util.NetworkUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.tencent.rtmp.TXLivePlayer;
import com.tencent.rtmp.ui.TXCloudVideoView;

/**
 * 视频回放
 * 
 * @author why
 * 
 */
public class FansRePlayRoomActivity extends BaseActivity {
	public static final String TAG = FansRePlayRoomActivity.class
			.getSimpleName();
	private TXLivePlayer mLivePlayer;
	private ImageButton ib_back;
	private String channel;
	private TXCloudVideoView mPlayerView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_fans_replay_room);
		ib_back = (ImageButton) findViewById(R.id.ib_back);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		ib_back.setOnClickListener(this);

		// mPlayerView即step1中添加的界面view
		mPlayerView = (TXCloudVideoView) findViewById(R.id.video_view);
		// 创建player对象
		mLivePlayer = new TXLivePlayer(context);
		// 关键player对象与界面view
		mLivePlayer.setPlayerView(mPlayerView);
		mPlayerView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			}
		});

		channel = getIntent().getStringExtra("channel");
		InRoom();
	}

	/** 进入房间 */
	private void InRoom() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("channel", channel);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_REPLAY_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "InRoom", response.toString());

						int code = JsonUtils.getInt(response, "code", -1);
						//
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());

							JSONArray url = JsonUtils.getJSONArray(data,
									"transcodeList", new JSONArray());
							String data_flv = JSONParser
									.getReplayInLiveRomm(url);
							mLivePlayer.startPlay(data_flv,
									TXLivePlayer.PLAY_TYPE_VOD_MP4);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							String message = JsonUtils.getString(data,
									"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mLivePlayer.stopPlay(true); // true代表清除最后一帧画面
		mPlayerView.onDestroy();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.ib_back:
			finish();
			break;
		default:
			break;
		}
	}

}
